<template>
  <div class="tags-container">
    <el-card>
      <!-- 表单区域 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="标签名称">
          <el-input v-model="formInline.tagName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
            <!--  v-for="item in tableData"
              :key="item.id"
              :label="item.state ? '禁用' : '启用'"
              :value="item.state" -->
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearHandler">清除</el-button>
          <el-button type="primary" @click="searchHandler">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" class="btn" @click="addHandler">
            <i class="el-icon-edit"></i>新增标签
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 灰色数据数量 -->
      <div
        style="
          font-size: 13px;
          color: #909399;
          width: 100%;
          background-color: #f4f4f5;
          padding: 8px;
        "
      >
        <span style="line-height: 18px; border-radius: 5px">
          <i
            class="el-icon-info"
            style="margin-right: 5px; font-size: 15px"
          ></i>
          数据一共{{ count }}条
        </span>
      </div>
      <!-- 表格区域 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
          <!-- <template slot-scope="scope"> -->
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            {{ dayFormat(scope.row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            {{ scope.row.state === 0 ? '已禁用' : '已启用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 启用禁用按钮 -->
            <el-button type="text" size="mini" @click="handleEdit(scope.row)">
              {{ scope.row.state === 0 ? '启用' : '禁用' }}
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="editHandler(scope.row)"
              :disabled="Boolean(scope.row.state)"
              >修改</el-button
            >
            <el-button
              :disabled="Boolean(scope.row.state)"
              size="mini"
              type="text"
              @click="delHandler(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div style="margin: 20px; text-align: right">
        <el-pagination
          :current-page="currentPage4"
          :page-sizes="[3, 5, 10, 15]"
          :page-size="pagTags.pagesize"
          layout="  prev, pager, next,sizes, jumper"
          :total="count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog
      :title="isEdit ? '修改目录' : '新增目录'"
      :visible.sync="dialogAddVisible"
      width="30%"
    >
      <tagsAdd
        v-if="dialogAddVisible"
        :row="row"
        :tableData="tableData"
        :isEdit="isEdit"
        :dialogAddVisible.sync="dialogAddVisible"
        @success="getTags"
      >
      </tagsAdd>
    </el-dialog>

    <TagsInterview />
  </div>
</template>

<script>
// import * as dayjs from 'dayjs'
import { dateinit } from '@/utils/index.js'
import tagsAdd from '../components/tags-add.vue'
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import TagsInterview from '@/components/TagsInterview'
export default {
  components: {
    TagsInterview,
    tagsAdd
  },
  data () {
    return {
      isSerach: false,
      row: {},
      isEdit: false,
      dialogAddVisible: false,
      formInline: {
        tagName: '',
        state: null,
        page: null,
        pagesize: null
      },
      tableData: [],
      // 默认页
      currentPage4: 1,
      count: 0, // 数量
      pagTags: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        subjectName: ''
      }
    }
  },

  created () {
    // 判断是否从学科页面跳转过来
    if (this.$route.query.id) {
      this.pagTags.subjectID = this.$route.query.id
      this.pagTags.subjectName = this.$route.query.name
    }
    console.log(this.$route.query.name, 777777777)

    this.getTags()
    console.log(this.pagTags, 1111)
  },

  methods: {
    // 111
    // 格式化时间
    dayFormat (time) {
      return dateinit(time)
    },
    // 页面数量变化的时候触发
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)

      if (this.isSerach) {
        this.formInline.pagesize = val
        this.searchHandler()
      } else {
        this.pagTags.pagesize = val
        this.getTags()
      }
    },
    // 当前页改变的时候触发
    handleCurrentChange (val) {
      // console.log(this.isSerach, '搜索值')
      if (this.isSerach) {
        // 搜索功能

        this.formInline.page = val
        this.searchHandler()
      } else {
        // console.log(`当前页: ${val}`)
        this.pagTags.page = val

        this.getTags()
      }
    },

    // 状态改变
    async handleEdit (row) {
      const editRow = {
        id: row.id
      }

      editRow.state = Number(!row.state)
      // editRow.state = !row.state

      const res = await changeState(editRow)
      if (res.status === 200) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        if (!this.isSerach) {
          this.getTags()
        } else {
          this.searchHandler()
        }
      }
    },
    // 删除
    delHandler (id) {
      // console.log(id, 777777)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          const res = await remove({ id: id })
          console.log(res, 44444)
          this.getTags()
        })
        .catch(() => {})
    },
    // 获取标签列表
    async getTags () {
      const res = await list(this.pagTags)
      console.log(res, 999)
      this.tableData = res.data.items
      this.count = res.data.counts
    },
    // 新增标签
    addHandler () {
      this.dialogAddVisible = true
      this.isEdit = false
    },
    // 修改标签
    editHandler (row) {
      this.isEdit = true
      this.dialogAddVisible = true
      this.row = row
    },
    // 搜索
    async searchHandler () {
      this.isSerach = true
      const res = await list(this.formInline)
      this.tableData = res.data.items
      this.count = res.data.counts
    },
    // 清除
    clearHandler () {
      this.formInline = {
        tagName: '',
        state: null
      }
    }
  }
}
</script>

<style scoped lang='scss'>
// .tags-container {
//   padding: 20px 30px;
// }
::v-deep.btn {
  margin-left: 350px;
}
::v-deep .el-dialog__body {
  padding: 15px 20px;
}
</style>
